<template>
  <div class="login">
    <van-nav-bar title="登录页面" left-arrow @click-left="$router.go(-1)" />
    <van-form>
      <van-field
        v-model="users.mobile"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="users.pwd"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" @click="login"
          >立即登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      users: {
        mobile: "",
        pwd: "",
      },
    };
  },
  created() {},
  mounted() {

  },
  methods: {
    //  执行登录
    login() {
      if (this.users.username == "" || this.users.password == "") {
        return false;
      }
      // 格式化为formdata
      let formdata = new FormData();
      formdata.append("mobile", this.users.mobile);
      formdata.append("pwd", this.users.pwd);
      this.$axios
        .post(
          "https://api.it120.cc/small4/user/m/login?deviceId=007&deviceName=monkey",
          formdata
        )
        .then((res) => {
          console.log(res);
          //   code码为500时，说明账号或密码错误请求失败
          if (res.code == 500) {
            this.$toast.fail(res.msg);
            return false;
          } else {
            //登录成功时
            this.$store.commit("login", res.data);
            this.$toast.success("登录成功");
            this.$router.go(-1)
          }
        });
    },
  },
  computed: {},
};
</script>

<style scoped></style>
